<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * Football Referees extension for Joomleague
 * @version $Id:playersranking.php 79 2008-02-12 15:58:00Z julien.vonthron $
 * @package JlgFootballReferees
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgFootballReferees is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */
?>

<?php

class ModelProjectReferees {

  var $_ref = null;
  var $_project;
  var $_db;
	
  function  ModelProjectReferees( & $database, & $project )
  {
  	$this->_db = $database;
    $this->_project = $project;
  }
  
  function getList()
  {
    if ( !$this->_ref ) {
    	// get referee of the project
	    $query = ' SELECT DISTINCT r.* FROM #__joomleague_referees as r'
	           . ' INNER JOIN #__joomleague_matches as m ON m.referee_id = r.id '
	           . ' WHERE m.project_id = ' .$this->_project->id
	           . ' ORDER BY lastname, firstname '
	           ;
	          
	    $this->_db->setQuery($query);
      $this->_ref = $this->_db->loadObjectList();
      
    }
    return $this->_ref;
  }
  
  function getGameCount()
  {
  	if ( !$this->_ref ) return null;
  	$ids = array();
  	foreach($this->_ref AS $r) {
  		$ids[]=$r->id;
  	}
      
    $query = ' SELECT COUNT(*) as `sum`, r.id FROM #__joomleague_referees as r'
             . ' INNER JOIN #__joomleague_matches as m ON m.referee_id = r.id '
             . ' WHERE m.project_id = ' .$this->_project->id
             . ' AND m.referee_id IN ('.implode(",", $ids).') '
             . ' AND matchpart1_result IS NOT NULL'
             . ' GROUP BY m.referee_id ';
    $this->_db->setQuery($query);
    return $this->_db->loadObjectList('id');
  }
  
  /**
   * return array of events (properties, stats, sum)
   *
   * @param int array $event_ids
   * @return array of object events (properties, stats, sum)
   */
  function getEvents($event_ids)
  {  
    $events = array();
    if (count($event_ids))
    {
      foreach($event_ids AS $ev)
      {
        $event = new stdclass;
        $event->eventtype = REHelper::getEventType($ev);
        $event->stats = $this->getEventCount($ev);
        $sum = 0;
        foreach ( $event->stats AS $s ) {
          $sum += $s->sum;
        }
        $event->sum = $sum;
        $events[$ev] = $event;
      }
    }
    return $events;
  }
  
  function getEventCount($event_id)
  {
    if ( !$this->_ref ) return null;
    $ids = array();
    foreach($this->_ref AS $r) {
      $ids[]=$r->id;
    }
    
    $query = ' SELECT SUM(event_sum) as `sum`, r.id FROM #__joomleague_referees as r'
             . ' INNER JOIN #__joomleague_matches as m ON m.referee_id = r.id '
             . ' INNER JOIN #__joomleague_match_events AS me ON me.match_id = m.match_id '
             . ' WHERE m.project_id = ' .$this->_project->id
             . ' AND m.referee_id IN ('.implode(",", $ids).') '
             . ' AND me.event_type_id = ' . $event_id
             . ' GROUP BY m.referee_id ';
    $this->_db->setQuery($query);
    return $this->_db->loadObjectList('id');
  }
}
?>
